function main_sim_v02
   
    %% initialize UI
    clear; % all;
    clear global;
    clc; 
    close all;
    tic;
    pause on;                   % activate pause function
    
    %% global variables
    global scene_size;        % size of square simulation scene
    global nodes_count;       % number of sensor nodes in simulation
    global radio_range;       % radio range
    global emit_energy;       % transmitting energy cost
    global emit_energy_red;   % transmitting energy cost with reduced cluster range
    global recv_energy;       % recieving energy cost
    global clust_range_ratio; % cluster reduced range ratio
    global BS_x;              % Base Station X position
    global BS_y;              % Base Station Y position
    global S_Map;             % Sensors map (see put_sensors.m)
    global D_Map;             % Distances map (see put_sensor.m)
    global N_Map;             % Neighbours map (see neighbouring.m)
    global used_energy;
    global energy_after_init;
    global iterations; 
    global hEdit1;
    global hEdit5;
    global hEdit6;
    global h2Edit5;
    global h2Edit6;
    global hRadio1;
    global hStart;
    global hBreak;
    global anim_loops;
    global F;
    global user_break;
    global user_pause;
    global fig1_draw_surf;

    %% global algorithm function's handlers
    global alg_init;            % algorithm initialization
    global alg_figs;            % creation of algorithm specific figures
    global alg_setup_msg_cycle; % setup message cycle
    global alg_msg_cycle_step;  % message cycle step
 
    
    %% adding folders
    addpath('./algorithms');
    addpath('./sim');
    addpath('./tools');
    addpath('./figures');

    %% initialization of global variables
    emit_energy=4;
    recv_energy=1;
    radio_range=20;             
    clust_range_ratio=0.6;         
    emit_energy_red=emit_energy*(clust_range_ratio*clust_range_ratio);
    scene_size=100;
    nodes_count=300;
    BS_x = 0;
    BS_y = 0;
    
    user_pause = 0;
    user_break = 0;
    
    fig1_draw_surf = true;

    %% initialization of local variables
    anim_loops=1;
    iterations=0;

    %% selection of algorithm
    % Avaible algorithms:
    % - test000
    % - mobile
    % - spin
    
    algorithm = 'localization';
    alg_param = 1;

    %% creating algorithm function handlers
    alg_init = str2func([algorithm '_init']);
    alg_figs = str2func([algorithm '_figs']);
    alg_setup_msg_cycle = str2func([algorithm '_setup_msg_cycle']);
    alg_msg_cycle_step = str2func([algorithm '_msg_cycle_step']);

    %% sensors placement
    [S_Map, D_Map] = put_sensors(nodes_count,scene_size,scene_size,BS_x,BS_y,20);

    %% neighbours calculation
    N_Map = neighbouring(nodes_count,radio_range,S_Map,D_Map);

    %% figures creation
    fig1();
    %fig2();
    alg_figs(alg_param);

    %%
    hold off;                  % erase old figure
    hold on;                   % add to existing (blank) figure
    %set(h2Edit5,'String',[algorithm '_' int2str(alg_param) ]);
    set(hEdit5,'String',[algorithm '_' int2str(alg_param) ]);

    %% Algorithm initialization
    alg_init(alg_param);
    energy_after_init = round(sum(S_Map(:,4))-sum(S_Map(:,3)));

    %% wait for user to start animation
    %while(user_pause)
    %end
    caxis([0 300]);
    axis([0  100  0  100 0 300]); %ustala zasieg osi
    F=getframe(gcf);
    first=1;
    visual=1;           
    alg_finished = 0;

    %% Main simulation loop: 
    for tt=first:1000; 
        
        %set(h2Edit6,'String',toc);
        set(hEdit1,'String',tt);  
        
        %% each cycle loop: sending messages to Base Station from each node
        
        for li=1:nodes_count;        % each node sends one message each turn
         
            
            if (S_Map(li,3)<emit_energy)
                % cannot send information from required node
                % alg_finished = 1;
                % break;
                
                % option nr 2 is to selected other still working node
                continue;
            end
            
            set(hEdit6,'String',[int2str(floor(toc)) 's']);
            iterations=iterations+1;   
            used_energy = round(sum(S_Map(:,4))-sum(S_Map(:,3)));  
            update_caption(1);  
            
            alg_setup_msg_cycle(alg_param,li);
            msg_cycle_finished = 0;  
            
            while (~(msg_cycle_finished || user_break))
                while (user_pause && ~user_break)
                end;
                [msg_cycle_finished, alg_finished] = alg_msg_cycle_step(alg_param);
            end;
            
            if visual==2
                update_caption(2);
                draw_f2();
            end;
           
            set(hRadio1,'Value',alg_finished~=1);  
        
            % film
            asa = getframe(gcf);
            F(anim_loops) = asa;
            anim_loops = anim_loops + 1;
            if (alg_finished || user_break)
                break;
            end;
        end;
        
        if (alg_finished || user_break)
            break;
        end;
    end;
   
    set(hRadio1, 'String', 'Saving film');
    set(hRadio1,'Value',false); 
    
    % on windows make compression
    % movie2avi(F,[algorithm '_' num2str(alg_param)],'compression','Cinepak', 'fps', 15,'quality', 100); 
    
    % on linux no compression allowed
    % movie2avi(F,[algorithm '_' num2str(alg_param)],'fps', 15,'quality', 100);

    set(hRadio1, 'String', 'Finished');
    set(hStart, 'Enable', 'inactive');
    set(hBreak, 'Enable', 'inactive');
    
    % remove added path to folders
    rmpath('./algorithms');
    rmpath('./sim');
    rmpath('./tools');
    rmpath('./figures');
  
end
